/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import command.Command;

import control.mapping.ControllerMapping;

/**
 * 
 * @author faubry
 */
public class Controller extends HttpServlet {

	/**
	 * autogénéré
	 */
	private static final long serialVersionUID = 2037837042695708835L;
	/*
	 * Mapping du site
	 */
	private HashMap<String, Command> commands = new HashMap<String, Command>();

	@Override
	public void init() {
//		// vers l'accueil
//		commands.put("id", new Index());
//		// vers facture
//		commands.put("facture", new Facture());
//		// vers validation d'un des bouton
//		commands.put("validation", new Validation());
//		// vers ajout d'un produit
//		commands.put("ajouterProduit", new AjouterProduit());
//		// vers vers retrait d'un produit
//		commands.put("retirerProduit", new RetirerProduit());
//		// vers vidage du panier
//		commands.put("viderPanier", new ViderPanier());
//		// vers login
//		commands.put("doLogin", new DoLogin());
//		// vers traitement du login
//		commands.put("login", new Login());
//		// vers logout du client
//		commands.put("logout", new Logout());
		
		//chargement de la HashMap à partir du fichier xml de config 
		commands = (new ControllerMapping(this.getServletContext().getRealPath("/")+this.getServletContext().getInitParameter("commands")).getMapping());
	}

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		/**** Command */

		// récupération du paramètre de commande
		String cmd = request.getParameter("cmd");
		// si cmd inexistant ou inconnu retour à l'index
		if (cmd == null || commands.get(cmd) == null) {
			cmd = "id";
		}

		// création de la commande
		Command commande = (Command) commands.get(cmd);
		String urlSuite = commande.execute(request, response);
		request.getRequestDispatcher(urlSuite).forward(request, response);
		/**** Command */
	}

	// <editor-fold defaultstate="collapsed"
	// desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>

}
